// packageMyInfo/pages/infoDetail/infoDetail.js
const app = getApp()
import { userFind,upload,download,userUpdate } from "../../../public/api/person.js"
import { throttle } from "../../../utils/util.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: '',
    faceData: '',
    name: '',
    photoId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserInfo()
  },

  onClickLeft() {
    wx.navigateBack({
      delta: 1
    })
  },

  //查询人员信息
  getUserInfo() {
    let userId = wx.getStorageSync('userInfo').id
    userFind({userId}).then(res => {
      if(res.data.code === 1) {
        let { name,photoId } = res.data.data
        this.setData({
          name,
          photoId: photoId ? photoId : '',
          userId
        })
        if(photoId) this.getPhoto(photoId)
        app.globalData.userInfo.name = name
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  // 查询照片
  getPhoto(photoId) {
    let data = {}
    data.id = photoId
    download(data).then(res => {
      if(res.statusCode === 200) {
        console.log(res.data);
        this.setData({faceData: res.data})
        app.globalData.userInfo.faceData = res.data
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  chooseAvatar() {
    let that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album'],
      success(res) {
        let filePath = res.tempFiles[0].tempFilePath
        console.log(res.tempFiles[0].tempFilePath)
        wx.getFileSystemManager().readFile({
          filePath,
          encoding: 'base64',
          success: rep => {
            let src = `${rep.data}`
            that.setData({
              faceData: src
            })
            that.getVoucherId()
          }
        })
      }
    })
  },

  // 上传图片获取id
  getVoucherId() {
    let timeStamp = new Date().getTime()
    let data = {}
    data.picBase64 = this.data.faceData
    data.name = "人员" + timeStamp
    upload(data).then(res => {
      if (res.data.code === 1) {
        console.log(res);
        this.setData({photoId: res.data.data})
        this.doUpdate()
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },

  // 修改个人信息
  doUpdate(type) {
    let that = this
    let data = {}
    data.userId = this.data.userId
    data.name = this.data.name
    data.photoId = this.data.photoId
    userUpdate(data).then(res => {
      if(res.data.code === 1) {
        that.getUserInfo()
        wx.showToast({
          title: '更新成功',
          icon: 'success',
          duration: 1500,
          success() {
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          }
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  doConfirm:throttle(function() {
    if(this.data.name === '') {
      wx.showToast({
        title: '请输入你的昵称',
        icon: 'none'
      })
      return false
    }
    this.doUpdate('name')
  }),

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})